package work.atpgroup.entity;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import java.util.Objects;

public class WantValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return Want.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"username","101","用户名不能为空");
        ValidationUtils.rejectIfEmpty(errors,"password","101","密码不能为空");
        ValidationUtils.rejectIfEmpty(errors,"schoolId","101","学号不能为空");
        ValidationUtils.rejectIfEmpty(errors,"identityPassword","101","密码不能为空");
        Want want = (Want)target;
        if(want.getUsername()!=null&&want.getUsername().length()<4){
            errors.rejectValue("username","101","用户名至少需要4个字符");
        }
        if(want.getPassword()!=null&&want.getPassword().length()<4){
            errors.rejectValue("password","101","密码最少需要4位");
        }
        if(want.getPassword()!=null&&want.getIdentityPassword()!=null&& !Objects.equals(want.getPassword(), want.getIdentityPassword())){
            errors.rejectValue("identityPassword","101","两个密码不一致");
        }
        if(want.getSchoolId().length()!=10){
            errors.rejectValue("schoolId","101","学号不正确");
        }
    }
}
